package meterMonitor.util.Timer;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 上一月的开始时间和结束时间
 */
public class StartAndEndTime {
    /**
     * 上月第一天
     * @return
     */
    public static String StartTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstDay;
        //获取前月的第一天
        Calendar cal_1= Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
        firstDay = format.format(cal_1.getTime());
        return firstDay;
    }

    /**
     * 上月最后一天
     * @return
     */
    public static String EndTime(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String lastDay;
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        lastDay = format.format(cale.getTime());
        return lastDay;
    }
//获取传入日期所在月的第一天
    public static Date getFirstDayDateOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        return cal.getTime();
    }

    //获取传入日期所在月的最后一天
    public static Date getLastDayOfMonth(final Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        return cal.getTime();
    }
    //得到当月一号
    public static String getMonth01(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-01");
        String date = formatter.format(new Date());
        return date;
    }

    /**
     * 得到当前时间
     */
    public static String getCurrentTime(){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String date = formatter.format(new Date());
        return date;
    }

    /**
     * 获取前一天的日期
     * @return
     */
    public static String getTheDayBefore(){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        String theDayBefore=df.format(calendar.getTime());
        return theDayBefore;
    }
}
